class ArtistsController < ApplicationController
  # GET /artists
  # GET /artists.xml
  def index
    @artists = Artist.all(:order => "name")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @artists }
    end
  end

  # GET /artists/1
  # GET /artists/1.xml
  def show
    @artist = Artist.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @artist }
    end
  end

  # GET /artists/new
  # GET /artists/new.xml
  def new
    @artist = Artist.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @artist }
    end
  end

  # GET /artists/1/edit
  def edit
    @artist = Artist.find(params[:id])
  end

  # POST /artists
  # POST /artists.xml
  def create
    @artist = Artist.new(params[:artist])

    success = false;
    artistother = Artist.find_by_name(@artist.name)
    if artistother == nil
      success = true
    end

    if !success
      respond_to do |format|
        @artist.errors.add('' , 'Artist with such name already exists.')
        format.html { render :action => "new" }
      end
    end

    if success
      respond_to do |format|
        if @artist.save
          flash[:notice] = 'Artist was successfully created.'
          format.html { redirect_to(@artist) }
          format.xml  { render :xml => @artist, :status => :created, :location => @artist }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @artist.errors, :status => :unprocessable_entity }
        end
      end
    end
  end

  # PUT /artists/1
  # PUT /artists/1.xml
  def update
    @artist = Artist.find(params[:id])

    artistname = @artist.name

    paramname = params[:artist][:name]
    success = false
    if (paramname == artistname)
      success = true
    end

    if !success
      artistother = Artist.find_by_name(paramname)
      if artistother == nil
        success = true
      end
    end

    if !success
      respond_to do |format|
        @artist.errors.add('' , 'Artist with such name already exists.')
        @artist.name = paramname
        format.html { render :action => "edit" }
      end
    end

    if success
      respond_to do |format|
        if @artist.update_attributes(params[:artist])
          flash[:notice] = 'Artist was successfully updated.'
          format.html { redirect_to(@artist) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @artist.errors, :status => :unprocessable_entity }
        end
      end
    end

  end

  # DELETE /artists/1
  # DELETE /artists/1.xml
  def destroy
    @artist = Artist.find(params[:id])
    @artist.destroy

    respond_to do |format|
      format.html { redirect_to(artists_url) }
      format.xml  { head :ok }
    end
  end
end
